package com.zhku.onlineshop.Servlet;

import com.zhku.onlineshop.Pojo.CartItem;
import com.zhku.onlineshop.Pojo.Result;
import com.zhku.onlineshop.Service.CartService;
import com.zhku.onlineshop.dto.CartItem_Product;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Slf4j
public class CartServlet {

    @Autowired
    private CartService cartService;

    @PostMapping("/AddIntoCart")
    public Result AddIntoCart(@RequestBody CartItem cartItem){
        log.info("把商品添加到购物车");
        if (cartService.AddIntoCart(cartItem.getUser_id(), cartItem.getProduct_id()))
            return Result.success();
        else
            return Result.error("商品添加购物车失败");
    }
    //商品数量减少
    @PostMapping("/ReduceProductNum")
    public Result ReduceProductNum(@RequestBody CartItem cartItem){
        log.info("商品数量减1");
        if(cartService.ReduceProductNum(cartItem.getUser_id(),cartItem.getProduct_id()))
            return Result.success();
        else
            return Result.error("减少失败");
    }

    //删除购物车商品
    @PostMapping("/DelProduct")
    public Result DelProduct(@RequestBody CartItem cartItem){
        log.info("购物车商品删除");
        if(cartService.DelProduct(cartItem.getUser_id(),cartItem.getProduct_id()))
            return Result.success();
        else
            return Result.error("删除失败");
    }

    @PostMapping("/GetCartByuserID")
    public Result GetCartByID(@RequestBody CartItem cartItem){
        log.info("根据当前用户id返回购物车列表");
        List<CartItem_Product> cartItem_product = cartService.GetCartByID(cartItem.getUser_id());
        if(cartItem_product != null){
            return Result.success(cartItem_product);
        }
        else
            return Result.error("当前购物车为空");
    }
}
